﻿using System;
using System.Collections.Generic;
using System.Web;
using Newtonsoft.Json;
using eCommerce.core;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.db.DataRespository;
using eCommerce.log;

namespace eCommerce.db.Action
{
    public class CommentAction : IBaseAction
    {
        public void Do(HttpContext context)
        {
            var actionName = Utils.GetIdFromRequestString(Utils.Action, string.Empty);

            var responseData = new ResponseData();
            switch (actionName)
            {
                case "listcomment":
                    responseData = GetListComment();
                    break;
                
                case "create":
                    responseData = Create();
                    break;               

                case "":
                    responseData.Success = false;
                    responseData.Message = "Can not found this action.";
                    break;
            }

            var jSonString = JsonConvert.SerializeObject(responseData, Utils.SerializeFormat("MM/dd/yyyy HH:mm:ss"));
            if (!string.IsNullOrEmpty(context.Request.Params["callback"]))
                jSonString = string.Format("{0}({1})", context.Request.Params["callback"], jSonString);
            context.Response.Write(jSonString);
        }

        private static ResponseData GetListComment()
        {
            var responseData = new ResponseData();
            var id          = Utils.GetIdFromRequestString(Utils.Id, 0);
            var typeId      = Utils.GetIdFromRequestString(Utils.TypeId, (int)CommentType.Article);
            var pageIndex   = Utils.GetIdFromRequestString(Utils.PageIndex, Constant.PageIndex);
            var pageSize    = Utils.GetIdFromRequestString(Utils.PageSize, Constant.LittlePageSize);

            var totalRecords = 0;

            if (typeId > 0)
            {
                try
                {
                    var lstComment = new List<CommentEntity>();
                    CommentBo.PaginationComment(id, typeId, pageIndex, pageSize, ref totalRecords);
                    responseData.Success    = true;
                    responseData.Data       = lstComment;
                    responseData.TotalRow   = totalRecords;
                }
                catch (Exception ex)
                {
                    responseData.Success = false;
                    responseData.Message = ex.Message;
                    Logger.WriteLog(Logger.LogType.Error, ex.Message);
                }
            }

            return responseData;
        }

        private static ResponseData Create()
        {
            var responseData = new ResponseData();

            var id       = Utils.GetIdFromRequestString(Utils.Id, 0);
            var parentId = Utils.GetIdFromRequestString(Utils.ParentId, 0);            
            var typeId   = Utils.GetIdFromRequestString(Utils.TypeId, (int)CommentType.Article);
            var status   = (int)CommentType.IsWaitingForPublish;
            var username = Utils.GetIdFromRequestString(Utils.UserName, string.Empty);
            var email    = Utils.GetIdFromRequestString(Utils.Email, string.Empty);
            var avatar   = "/images/user-1.jpg";
            var body     = Utils.GetIdFromRequestString(Utils.Content, string.Empty);
            
            try
            {
                var o = new CommentEntity();
                
                if (id > 0)
                {
                    o.Id = id;
                    o.ParentId  = parentId;
                    o.TypeId    = typeId;
                    o.Status    = status;
                    o.UserName  = username;
                    o.Email     = email;
                    o.Avatar    = avatar;
                    o.Body      = body;                    
                }                       
                
                CommentBo.Create(o);

                responseData.Success = true;
                responseData.Data = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }
    }
}
